package com.yzy.soft.system.controller;


import com.google.common.collect.Lists;
import com.yzy.soft.core.base.controller.BaseController;
import com.yzy.soft.core.base.tips.ErrorTip;
import com.yzy.soft.core.base.tips.Tip;
import com.yzy.soft.system.dto.TableDataDto;
import com.yzy.soft.common.persistence.dao.RuleversionMapper;
import com.yzy.soft.common.persistence.model.Ruleversion;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yinzhiyuan
 * @since 2017-11-29
 */
@Api(value = "RulesversionController", description = "版本控制接口")
@RestController
@RequestMapping("/rulesversion")
public class RulesversionController extends BaseController{

    @Autowired
    private RuleversionMapper ruleversionMapper;

    @ApiOperation("查询版本信息列表")
    @ApiImplicitParams({@ApiImplicitParam(name="page",value="当前页数",dataType="int", paramType = "query"),@ApiImplicitParam(name="limit",value="页数据条数",dataType="int", paramType = "query")})
    @GetMapping("/list")
    public TableDataDto getAll( @RequestParam(name="page",defaultValue = "1",required = true) int page, @RequestParam(name="limit",defaultValue = "10",required = true) int pageSize) {
        page=(page-1)*pageSize;
        pageSize=page+pageSize;
        List<Ruleversion> result=ruleversionMapper.getState1(page,pageSize);
        TableDataDto tableDataDto=new TableDataDto();
        tableDataDto.setCode("00");
        tableDataDto.setMsg("000");
        tableDataDto.setCount(ruleversionMapper.getCount().intValue());
        tableDataDto.setData(result);
        return tableDataDto;
    }
    @ApiOperation("查询版本信息列表")
    //@ApiImplicitParams({@ApiImplicitParam(name="page",value="当前页数",dataType="int", paramType = "query"),@ApiImplicitParam(name="limit",value="页数据条数",dataType="int", paramType = "query")})
    @GetMapping("/{id}")
    public Ruleversion getById(@ApiParam @PathVariable("id") String id) {
        return ruleversionMapper.selectById(id);
    }

    @ApiOperation("添加版本信息")
    @PostMapping("/add")
    public Tip add(@ModelAttribute Ruleversion rulesversion) {
        rulesversion.setUserflag(0);
        rulesversion.setCreate(new Date());
        rulesversion.setUpdate(new Date());
        Integer result=ruleversionMapper.insert(rulesversion);
        return SUCCESS_TIP;
    }
    @ApiOperation("删除版本信息")
    @PostMapping("/delete")
    public Tip batchDelete(@RequestParam("ids") String[] ids) {

//        Integer result=ruleversionMapper.status(firulesversion);
        return SUCCESS_TIP;
    }
    @ApiOperation("根据版本编号删除版本信息")
    @DeleteMapping("/{id}")
    public Tip delete(@RequestParam(name="ids[]",required = false) String[] rulesversion,@PathVariable(name="id",required = false) String id) {
        if(StringUtils.isNotBlank(id)){
            Integer result=ruleversionMapper.deleteById(id);
        }else if(rulesversion.length!=0){
            Integer result=ruleversionMapper.deleteBatchIds(Lists.newArrayList(rulesversion) );
        }
        return SUCCESS_TIP;
    }

    @ApiOperation("批量删除版本信息")
    @DeleteMapping("/")
    public Tip delete1(@RequestParam(name="ids[]",required = false) String[] rulesversion) {
        if(rulesversion.length!=0){
            Integer result=ruleversionMapper.deleteBatchIds(Lists.newArrayList(rulesversion) );
        }
        return SUCCESS_TIP;
    }
    @ApiOperation("批量版本信息设置成弃用状态")
    @PostMapping("/batchNuUse")
    public Tip batchNuUse(@RequestParam(name="ids[]",required = false) String[] rulesversion) {
        if(rulesversion.length!=0){
            Integer result=ruleversionMapper.batchNuUseIds(Lists.newArrayList(rulesversion) );
        }
        return SUCCESS_TIP;
    }
    @ApiOperation("批量将版本信息设置成可用状态")
    @PostMapping("/batchUse")
    public Tip batchUse(@RequestParam(name="ids[]",required = false) String[] rulesversion) {
        if(rulesversion.length!=0){
            Integer result=ruleversionMapper.batchUseIds(Lists.newArrayList(rulesversion) );
        }
        return SUCCESS_TIP;
    }

    @PutMapping("/status/{id}")
    public Tip status(@PathVariable("id")String id) {
        Integer result=ruleversionMapper.status(id);
        if(null==result){
            return new ErrorTip(500,"s");
        }
        return SUCCESS_TIP;
    }
    @ApiOperation("更新版本信息")
    @PutMapping("")
    public Tip update(@ModelAttribute Ruleversion ruleversion) {
        ruleversion.setUpdate(new Date());
        Integer result=ruleversionMapper.updateById(ruleversion);
        if(null==result){
            return new ErrorTip(500,"s");
        }
        return SUCCESS_TIP;
    }
}
